﻿using UnityEngine;
using UnityEngine.Rendering;
using Yoozoo.UI.YGUI;

public class SliderColor : MonoBehaviour
{
 
    Color32[] colors = new Color32[]{
          new Color32(1, 0, 0,1),
          new Color32(1, 1, 0,1),
          new Color32(0, 1, 0,1),
          new Color32(0, 1, 1,1),
          new Color32(0, 0, 1,1),
          new Color32(1, 0, 1,1),
          new Color32(1, 0, 0,1)};
          
    private YSlider slider;
    private YText txt_color;
    public uint color_val;
    private string tag = "[C#][Character] SliderColor - ";

    void Awake()
    {
        slider = GetComponent<YSlider>();
        slider.onValueChanged.AddListener(OnColorSliderValueChanged);
        
        txt_color = slider.text.GetComponent<YText>();
        
        var hueTex = new Texture2D(colors.Length, 1);
        hueTex.SetPixels32(colors);
        hueTex.Apply();
        
        slider.transform.Find("Background").GetComponent<YImage>().sprite = 
            Sprite.Create(hueTex, new Rect(Vector2.zero, new Vector2(colors.Length, 1)), Vector2.one * 0.5f);
    }
    
    void OnColorSliderValueChanged(float _value)
    {
        Debug.Log(string.Format("{0}Changed ---- OnColorSliderValueChanged = {1}", tag, _value));
        Color color = Color.HSVToRGB(_value, 1, 1);
        Debug.Log(string.Format("{0}Changed ---- color = {1}, {2}, {3}, {4}", tag, color.r, color.g, color.b, color.a));
        color_val = ColorUtils.ToHex(color);
        Debug.Log(string.Format("{0}Changed ---- value = {1}", tag, color_val));
        if (txt_color != null) {
            txt_color.text = string.Format("#{0}", ColorUtility.ToHtmlStringRGB(color));
        }
    }

    public void OnSliderColorUpdate(uint hex)
    {
        Debug.Log(string.Format("{0}Update +++++ OnSliderColorUpdate = {1}", tag, hex));
        float h, s, v;
        Color color = ColorUtils.ToRGBA(hex);
        Debug.Log(string.Format("{0}Update +++++ color = {1}, {2}, {3}, {4}", tag, color.r, color.g, color.b, color.a));
        Color.RGBToHSV(color, out h, out s, out v);
        Debug.Log(string.Format("{0}Update +++++ hsv = {1}, {2}, {3}", tag, h, s, v));
        OnColorSliderValueChanged(h);
    
    }
}